from captcha.fields import CaptchaField, CaptchaTextInput
from django import forms
from .models import CustomUser

class UserPassWordChangeForm(forms.Form):
    old_password = forms.CharField(label="原始密码", widget=forms.PasswordInput(attrs={
        "class": "form-control",
        "placeholder": "请输入密码"
    }))
    password = forms.CharField(label="密码", widget=forms.PasswordInput(attrs={
        "class": "form-control",
        "placeholder": "请输入密码"
    }))
    password2 = forms.CharField(label="确认密码", widget=forms.PasswordInput(attrs={
        "class": "form-control",
        "placeholder": "请再次输入密码"
    }))


    def clean_password(self):
        password = self.cleaned_data.get('password')
        if len(password) < 6:
            raise forms.ValidationError("密码长度要大于6位")
        return password

    def clean_password2(self):
        password = self.cleaned_data.get('password')
        password2 = self.cleaned_data.get('password2')
        if password != password2:
            raise forms.ValidationError("密码不一致")
        return password2





class UserLoginForm(forms.Form):
    username = forms.CharField(label="用户名/手机号", widget=forms.TextInput(attrs={
        "class": "form-control",
        "placeholder": "请输入用户名/邮箱"

    }))
    password = forms.CharField(label="密码", widget=forms.PasswordInput(attrs={
        "class": "form-control",
        "placeholder": "请输入密码"
    }))


class UserRegisterForm(forms.Form):
    password = forms.CharField(label="密码", widget=forms.PasswordInput(attrs={
        "class": "form-control",
        "placeholder": "请输入密码"
    }))
    password2 = forms.CharField(label="确认密码", widget=forms.PasswordInput(attrs={
        "class": "form-control",
        "placeholder": "请再次输入密码"
    }))
    phone = forms.CharField(label="手机号", required=True, widget=forms.TextInput(attrs={
        "class": "form-control",
        "placeholder": "请输入手机号",
        "maxlength": "11"
    }))
    code = forms.CharField(label="验证码", required=True, widget=forms.TextInput(attrs={
        "class": "form-control",
        "placeholder": "请输入验证码"
    }))
    class Meta:
        model = CustomUser
        fields = []
    def clean_phone(self):
        phone = self.cleaned_data.get('phone')
        # 验证手机号格式
        import re
        if not re.match(r'^1[3-9]\d{9}$', phone):
            raise forms.ValidationError("手机号格式不正确")
        # 检查手机号是否已被注册
        if CustomUser.objects.filter(username=phone).exists():
            raise forms.ValidationError("该手机号已被注册")
        return phone

    def clean_password(self):
        password = self.cleaned_data.get('password')
        if not password:
            raise forms.ValidationError("请输入密码")
        if len(password) < 6:
            raise forms.ValidationError("密码长度要大于6位")
        return password

    def clean_password2(self):
        password = self.cleaned_data.get('password')
        password2 = self.cleaned_data.get('password2')
        if password != password2:
            raise forms.ValidationError("密码不一致")
        return password2




class UserInfoChangeForm(forms.ModelForm):
    class Meta:
        model = CustomUser
        fields = ['username', 'head']
        widgets = {
            'username': forms.TextInput(attrs={
                "class": "form-control",
                "placeholder": "请输入用户名"
            }),
            'head': forms.FileInput(attrs={})

        },
        labels = {
            'head': "头像"
        }

    def clean_head(self):
        head = self.cleaned_data.get('head')
        print(head, "0000")
        return head
